package org.flexthinker.binding.form.support
{
    import org.flexthinker.binding.value.ValueModel;
    import org.flexthinker.binding.value.support.MockBean;

    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;

    /**
     *
     */
    public class DefaultFormModelTest
    {
        private var fm:DefaultFormModel;
        private var bean:MockBean;

        public function DefaultFormModelTest()
        {
        }

        [Before]
        public function setUp():void
        {
            bean = new MockBean();
            bean.bindableProperty = "SOME CLEAN PROP";
        }

        [Test]
        public function testFormBecomesDirtyWhenValueModelUpdates():void
        {
            fm = new DefaultFormModel(bean, true);
            assertThat("form shouldn't be dirty initally", fm.isDirty(), equalTo(false));
            fm.add( "bindableProperty" );
            assertThat("form shouldn't be dirty after adding a new property", fm.isDirty(), equalTo(false));

            bean.bindableProperty = "SOME NEW CLEAN PROP";
            assertThat("form shouldn't become dirty when domainObject updates", fm.isDirty(), equalTo(false));
            assertThat("formModel should reflect changed in domainObject", fm.getValueModel("bindableProperty").getValue(), equalTo(bean.bindableProperty));

            fm.getValueModel("bindableProperty").setValue("SOME DIRTY PROP");
            assertThat("formModel should become dirty when changing properties", fm.isDirty(), equalTo(true));
        }

        [Test]
        public function testFormModelBecomesCleanWhenFormObjectUpdates():void
        {
            fm = new DefaultFormModel(bean, true);
            fm.add( "bindableProperty" );

            bean.bindableProperty = "SOME NEW CLEAN PROP";
            assertThat("form shouldn't become dirty when domainObject updates", fm.isDirty(), equalTo(false));
            assertThat("formModel should reflect changed in domainObject", fm.getValueModel("bindableProperty").getValue(), equalTo(bean.bindableProperty));

            fm.getValueModel("bindableProperty").setValue("SOME DIRTY PROP");
            assertThat("formModel should become dirty when changing properties", fm.isDirty(), equalTo(true));

            bean.bindableProperty = "SOME OTHER CLEAN PROP";
            assertThat("formModel should become clean when domainObject changes", fm.isDirty(), equalTo(false));
        }

        [Test]
        public function testFormModelRevert():void
        {
            fm = new DefaultFormModel(bean, true);
            fm.add( "bindableProperty" );

            var origVal:String = fm.getValueModel("bindableProperty").getValue().toString();

            fm.getValueModel("bindableProperty").setValue("SOME DIRTY PROP");
            assertThat("formModel should become dirty when changing properties", fm.isDirty(), equalTo(true));

            fm.revert();
            assertThat("formModel should be clean after revert()", fm.isDirty(), equalTo(false));
            assertThat("ValueModel should be identical with the formObject", fm.getValueModel("bindableProperty").getValue(), equalTo(bean.bindableProperty));
            assertThat("ValueModel should revert too", fm.getValueModel("bindableProperty").getValue(), equalTo(origVal));
        }
    }
}